package com.wan.data.mysql.orm.convert.auto;

import com.wan.data.mysql.orm.convert.AutoConverter;
import com.wan.data.mysql.orm.meta.ColumnMeta;
import com.wan.data.mysql.util.DbColumnType;

import java.util.concurrent.atomic.AtomicLong;

public class LongConverter implements AutoConverter {
    @Override
    public Class<?>[] convertType() {
        return new Class[]{long.class,Long.class, AtomicLong.class};
    }

    @Override
    public DbColumnType getDbType() {
        return DbColumnType.BIGINT;
    }

    @Override
    public Object dbToEntity(ColumnMeta columnMeta, Object value) throws Exception {
        if(columnMeta.getFieldType() == AtomicLong.class){
            return new AtomicLong((Long) value);
        }
        return value;
    }

    @Override
    public Object entityToDb(ColumnMeta columnMeta, Object value) throws Exception {
         if(value instanceof AtomicLong){
             AtomicLong atomicLong = (AtomicLong) value;
             return atomicLong.get();
         }
         return value;
    }
}
